package com.crr.mall.product.web;

import com.crr.mall.product.entity.CategoryEntity;
import com.crr.mall.product.service.CategoryService;
import com.crr.mall.product.vo.Catalog2VO;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient redisson;
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.queryLevelByOne();
        //视图解析器进行拼接
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String, List<Catalog2VO>> getCategoryJson() {
        Map<String, List<Catalog2VO>> catalogJSON = categoryService.getCatalogJSON();
        return catalogJSON;
    }
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //获取一把锁 只要锁的名字一样就是同一把锁
        RLock mylock = redisson.getLock("mylock");
        //加锁
        mylock.lock();//阻塞式等待
        try {
            System.out.println("加锁成功，执行业务"+Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //解锁
            System.out.println("释放锁"+Thread.currentThread().getId());
            mylock.unlock();
        }

        return "hello";
    }
    @ResponseBody
    @GetMapping("/write")
    public String write(){
        //保证能读到最新的数据
        String s = null;
        RReadWriteLock rwLock = redisson.getReadWriteLock("rwLock");
        RLock rLock = rwLock.writeLock();
        try {
            //改数据加写锁

            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @ResponseBody
    @GetMapping("/read")
    public String read(){
        String s = null;
        RReadWriteLock rwLock = redisson.getReadWriteLock("rwLock");
        RLock rLock = rwLock.readLock();
        try {
            //读数据加读锁
            rLock.lock();
          s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

}
